<?php
/**
 * User: Hufeng
 * Date: 2017/12/04 17:51
 * Desc: 学堂
 */
namespace App\Controller;
use App\Model\CourseModel;
use App\Model\CourseTypeModel;

class School extends Base {

    protected $cModel;
    protected $ctModel;
    public function __construct(){
        parent::__construct();
        $this->cModel = new CourseModel();
        $this->ctModel = new CourseTypeModel();
    }

    //学堂首页(一二级分类)
    public function index(){
        $cQuery = $this->cModel->queryData(['status'=>1],['one_typeid','two_typeid']);
        $tidArr = [];
        foreach ($cQuery as $val){
            $tidArr[] = $val['one_typeid'];
            $tidArr[] = $val['two_typeid'];
        }
        $res = [];
        if($tidArr){
            $ctQuery = $this->ctModel->queryData(['id'=>$tidArr,'status'=>1,'ORDER'=>['pid'=>'ASC','sort'=>'DESC']]);
            $data = [];
            foreach ($ctQuery as $val){
                $arr = [];
                $arr['id'] = $val['id'];
                $arr['name'] = $val['name'];
                if($val['pid']){
                    $data[$val['pid']]['child'][] = $arr;
                }else{
                    $data[$val['id']] = $arr;
                }
            }
            foreach ($data as $val){
                $res[] = $val;
            }
        }
        $this->outData['data'] = $res;
        return $this->fjson($this->outData);
    }
    //学堂章节页
    public function chapter(){
//        $this->param['typeId'] = 2;
        $needParam = array(
            'typeId'=>array('msg'=>'typeId参数异常','type'=>'int')
        );
        $this->regArguments($needParam,$this->param);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $cWhere = ['two_typeid'=>$this->param['typeId'],'status'=>1,'ORDER'=>['sort'=>'DESC']];
        $cQuery = $this->cModel->queryData($cWhere,['id','title','info']);
        $ctRes = $this->ctModel->findData(['id'=>$this->param['typeId']]);
        $this->outData['typeName'] = $ctRes['name'];
        $this->outData['data'] = $cQuery;
        return $this->fjson($this->outData);
    }
    //学堂内容页
    public function info(){
//        $this->param['id'] = 2;
        $needParam = array(
            'id'=>array('msg'=>'id参数异常','type'=>'int')
        );
        $this->regArguments($needParam,$this->param);
        if($this->outData['code']){
            return $this->fjson($this->outData);
        }
        $cWhere = ['id'=>$this->param['id'],'status'=>1];
        $cQuery = $this->cModel->findData($cWhere,['id','title','cover_img','content']);
        $this->outData['data'] = $cQuery;
        return $this->fjson($this->outData);
    }

}
